package org.xutils.http;

import android.app.Application;

import org.xutils.db.DbManager;
import org.xutils.db.ex.DbException;
import org.xutils.db.xDb;
import org.xutils.http.common.TaskController;
import org.xutils.http.common.task.TaskControllerImpl;
import org.xutils.http.manager.HttpManager;
import org.xutils.http.manager.impl.HttpManagerImpl;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;

/**
 * Created by ZZH on 4/20/21.
 *
 * @Date: 4/20/21
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public class xHttp {

    private static xHttp mInstance;

    private xHttp() {
    }


    public static xHttp getInstance() {
        if (mInstance == null) {
            synchronized (xHttp.class) {
                if (mInstance == null) {
                    mInstance = new xHttp();
                }
            }
        }
        return mInstance;
    }

    public static TaskController task() {
        return taskController;
    }

    public static HttpManager http() {
        if (xHttp.httpManager == null) {
            HttpManagerImpl.registerInstance();
        }
        return xHttp.httpManager;
    }

    /**
     * 使用之前需要先初始化
     *
     * @param ctx 传入当前app实例，默认会把数据库依赖实例化
     */
    public void init(Application ctx) {
        TaskControllerImpl.registerInstance();
        xDb.getInstance().init(ctx);
    }

    public static DbManager getDb(DbManager.DaoConfig daoConfig) throws DbException {
        return xDb.getDb(daoConfig);
    }

    public static Application app() {
        return xDb.getInstance().getContext();
    }

    private static TaskController taskController;
    private static HttpManager httpManager;

    public static void setTaskController(TaskController taskController) {
        if (xHttp.taskController == null) {
            xHttp.taskController = taskController;
        }
    }

    public static void setHttpManager(HttpManager httpManager) {
        xHttp.httpManager = httpManager;
    }

    public static void setDefaultHostnameVerifier(HostnameVerifier hostnameVerifier) {
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    }

}
